package com.sky.demo.file;

import org.apache.http.entity.ContentType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;


public class FileToMultipartFile {
    private static final Logger logger = LoggerFactory.getLogger(FileToMultipartFile.class);

    public static MultipartFile transfer(File file){
        MultipartFile multipartFile = null;
        String fileName = file.getName();
        try {
            FileInputStream inputStream = new FileInputStream(file);
            multipartFile = new MockMultipartFile(fileName, fileName, ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream);
        } catch (Exception e) {
            logger.error("file transfer to multipartFile failed, error: " + e.getMessage());
        }
        return multipartFile;
    }
}
